package gervosou;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;

public class VueAjouterMandataireCompteSoc extends Vue {

	/**
	 * 
	 */
	//private static final long serialVersionUID = 5069349224425178639L;

	private CompteSociete _compte ;
	private Droit _droit;
	private JPanel contentPane;
	private JTextField textFieldNum;
	private JTextField textFieldNom;
	private JTextField textFieldPrenom;
	
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JButton btnRechercher;
	private JButton btnAnnuler;
	private JButton btnEnregistrer;
	private JTextField textFieldDn;
	private JTextField textFieldSoc;
	private JTextField textFieldMax;
	/**
	 * Create the frame.
	 */
	public VueAjouterMandataireCompteSoc(Controleur controleur) {
		super (controleur);
		setTitle("Enregistrement d'un nouveau compte pour un client existant");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 545, 437);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNum = new JLabel("Numero");
		lblNum.setBounds(12, 12, 61, 16);
		contentPane.add(lblNum);
		
		JLabel lblPrenom = new JLabel("Prenom :");
		lblPrenom.setBounds(12, 225, 61, 33);
		contentPane.add(lblPrenom);
		
		textFieldNum = new JTextField();
		textFieldNum.setBounds(330, 7, 134, 28);
		contentPane.add(textFieldNum);
		textFieldNum.setColumns(10);
		
		textFieldNom = new JTextField();
		textFieldNom.setColumns(10);
		textFieldNom.setBounds(124, 197, 134, 28);
		contentPane.add(textFieldNom);
		
		JLabel lblAdresse = new JLabel("Nom :");
		lblAdresse.setBounds(12, 197, 61, 16);
		contentPane.add(lblAdresse);
		
		textFieldPrenom = new JTextField();
		textFieldPrenom.setColumns(10);
		textFieldPrenom.setBounds(124, 237, 134, 28);
		contentPane.add(textFieldPrenom);
		
		
		btnRechercher = new JButton("Rechercher");
		btnRechercher.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String num = textFieldNum.getText();
				// liaison de la vue avec l'objet observe
				if (textFieldNum.getText().isEmpty()){
					Message dialog = new Message("Numero de compte obligatoire");
					dialog.setVisible(true);
					}
				else{
						_compte = getControleur().rechCompte(Integer.valueOf(num));
					}
				}
			});
		
		btnRechercher.setBounds(362, 49, 117, 29);
		contentPane.add(btnRechercher);
		
		btnAnnuler = new JButton("Annuler");
	
		btnAnnuler.setBounds(12, 370, 117, 29);
		btnAnnuler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().fermerVue(VueAjouterMandataireCompteSoc.this);
			}
		});
		contentPane.add(btnAnnuler);
		
		final JRadioButton rdbtnA = new JRadioButton("a");
		buttonGroup.add(rdbtnA);
		
		rdbtnA.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JLabel lblValeurMax = new JLabel("Valeur Max :");
				lblValeurMax.setBounds(286, 101, 83, 15);
				contentPane.add(lblValeurMax);
				textFieldMax.setEnabled(false);
				textFieldMax.setVisible(false);
				
			}}	
			
		);
		rdbtnA.setBounds(12, 345, 41, 23);
		contentPane.add(rdbtnA);
		
		final JRadioButton rdbtnB = new JRadioButton("b");
		buttonGroup.add(rdbtnB);
		rdbtnB.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JLabel lblValeurMax = new JLabel("Valeur Max :");
				lblValeurMax.setBounds(286, 101, 83, 15);
				contentPane.add(lblValeurMax);
				textFieldMax.setEnabled(true);
				textFieldMax.setVisible(true);
				
			}}	
			
		);
		
		rdbtnB.setBounds(56, 345, 33, 23);
		contentPane.add(rdbtnB);
		
		final JRadioButton rdbtnC = new JRadioButton("c");
		buttonGroup.add(rdbtnC);
		rdbtnC.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JLabel lblValeurMax = new JLabel("Valeur Max :");
				lblValeurMax.setBounds(286, 101, 83, 15);
				contentPane.add(lblValeurMax);
				textFieldMax.setEnabled(false);
				textFieldMax.setVisible(false);
				
				
			}}	
			
		);
		rdbtnC.setBounds(91, 345, 141, 23);
		contentPane.add(rdbtnC);
		
		btnEnregistrer = new JButton("Enregistrer");
		btnEnregistrer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String nom = textFieldNom.getText();
				String prenom = textFieldPrenom.getText();
				String date = textFieldDn.getText();
				String soc = textFieldSoc.getText();
				String val = textFieldMax.getText();
				String droit;
				if (rdbtnA.isSelected()) {
					droit = "a";}
				else if (rdbtnB.isSelected()) {
					droit = "b";}
				else {
					droit = "c";}
				getControleur().ajouterMandataireCompteSoc(_compte.getNumero(),nom,prenom,date,soc,droit,val);
			}}	
			
		);
		
		btnEnregistrer.setBounds(141, 370, 117, 29);
		contentPane.add(btnEnregistrer);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(29, 87, 477, 2);
		contentPane.add(separator);
		
		JLabel lblDateDeNaissance = new JLabel("date de naissance");
		lblDateDeNaissance.setBounds(12, 277, 117, 15);
		contentPane.add(lblDateDeNaissance);
		
		textFieldDn = new JTextField();
		textFieldDn.setBounds(142, 275, 114, 19);
		contentPane.add(textFieldDn);
		textFieldDn.setColumns(10);
		
		JLabel lblDroit = new JLabel("Droit :");
		lblDroit.setBounds(12, 322, 61, 15);
		contentPane.add(lblDroit);
		
		JLabel lblSocit = new JLabel("Société :");
		lblSocit.setBounds(12, 152, 61, 15);
		contentPane.add(lblSocit);
		
		textFieldSoc = new JTextField();
		textFieldSoc.setBounds(144, 150, 114, 19);
		contentPane.add(textFieldSoc);
		textFieldSoc.setColumns(10);
		
		JSeparator separator_1 = new JSeparator();
		separator_1.setOrientation(SwingConstants.VERTICAL);
		separator_1.setBounds(270, 87, 11, 324);
		contentPane.add(separator_1);
		
		
		
		textFieldMax = new JTextField();
		textFieldMax.setBounds(392, 101, 114, 19);
		contentPane.add(textFieldMax);
		textFieldMax.setColumns(10);
	}

	/**
	 * definition des etats de la fenetre
	 */
	public void setEtat (int etat){
		switch (etat) {
		case initiale: {
			textFieldSoc.setEnabled(false);
			textFieldPrenom.setEnabled(false);
			textFieldNom.setEnabled(false);
			textFieldDn.setEnabled(false);
			textFieldMax.setEnabled(false);
			textFieldMax.setVisible(false);
			
			
			btnEnregistrer.setVisible(false);
			break;
			}
		case finale: {
			btnRechercher.setEnabled(false);
			textFieldNum.setEditable(false);
			
			textFieldPrenom.setEnabled(true);
			textFieldNom.setEnabled(true);
			textFieldDn.setEnabled(true);
			btnEnregistrer.setVisible(true);
			btnEnregistrer.setEnabled(true);
			break;
			}
		}
	}
	
	private CompteSociete getCompteSoc() {
		return _compte;
	}
	
	private void setCompteSoc(CompteSociete compte) {
		 _compte = compte;
	}
	
	public void alimente(CompteSociete compte) {
		Societe soc=compte.getSociete();
		textFieldSoc.setText(soc.getNom());
	
	}
	
	public void setCompteSociete(CompteSociete compte) {
		
		_compte=compte;
		
	}
	
	public void setDroit(Droit droit) {
		
		_droit=droit;
		
	}
}


